﻿using System;
using System.Linq;

namespace Framework.Extensions
{
    public static class EnumExtensions
    {
        public static TAttribute GetAttribute<TAttribute>(this Enum source)
            where TAttribute : Attribute
        {
            var enumType = source.GetType();
            var enumMember = enumType
                .GetFields()
                .Single(field => field.FieldType == enumType && field.GetValue(source).Equals(source));

            return (TAttribute) enumMember.GetCustomAttributes(typeof (TAttribute), false).SingleOrDefault();
        }
    }
}